package dolf.objects.weapons.Sword;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.ImageIcon;

import dolf.game.Level;
import dolf.game.Player;
import dolf.objects.CircleObject;
import dolf.objects.ImageStore;
import dolf.objects.Point;
import dolf.objects.Ball;
//import dolf.objects.Vector;
import dolf.objects.WorldObject;
import dolf.objects.weapons.Weapon;
import dolf.window.SystemMessagesHandler;

public class Sword extends CircleObject implements Weapon, java.io.Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final int Price = 75;
	public static final String Name = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Sword.lang.lang").getString("name");
	public static final String WeaponType = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Sword.lang.lang").getString("type");
	public static final String Description = SystemMessagesHandler.getInstance().getMessages("dolf.objects.weapons.Sword.lang.lang").getString("description");
	public static final javax.swing.ImageIcon Icon = ImageStore.getInstance().getIcon("dolf/objects/weapons/Sword/gfx/icon.png");
	
	private final int MAX_ATTACK = 3;
	private final int TRICKER = 2;
	private AffineTransform affineTransform;
//	private Graphics2D graph_tmp;
	private BufferedImage image;
	private int attacks;
	private int tricker;
	private double rotation;
	private boolean attacking;
	private Player pl;
	private boolean inititalized = false;
	transient private Image img;
	public Sword(){
		super(null,20,false);
		this.setCollitabe(false);
		
		img = ImageStore.getInstance().getImage("dolf/objects/weapons/Sword/gfx/icon3.png");
		img = img.getScaledInstance(2*this.getRadius(), 2*this.getRadius(), Image.SCALE_SMOOTH);
		
		rotation = 0;
		attacks = MAX_ATTACK;
	}
	public void initialize(Player _pl){
		if(this.inititalized){
			return;
		}
		this.attacks = this.MAX_ATTACK;
		this.attacking = false;
		this.inititalized = true;
		this.pl = _pl;
		this.pl.setWeaponState();
		this.tricker = this.TRICKER;
	}
	public void deInitialize(){
		this.pl = null;
		this.inititalized = false;
	}
	public void paint(Graphics2D g){
		
        if (img == null) {
        	img = ImageStore.getInstance().getImage("dolf/objects/weapons/Sword/gfx/icon3.png");
    		img = img.getScaledInstance(2*this.getRadius(), 2*this.getRadius(), Image.SCALE_SMOOTH);
        }
        if(attacking){		
        	if(this.tricker <= 0){
        		int mask = BufferedImage.TYPE_3BYTE_BGR| BufferedImage.TYPE_INT_ARGB; 
        	
        		affineTransform = AffineTransform.getRotateInstance(this.rotation/180*Math.PI,this.getRadius(), this.getRadius());
            
        		image = new BufferedImage (2*this.getRadius(), 2*this.getRadius(),mask);
        		Graphics2D graph_tmp = (Graphics2D) image.getGraphics();

        		graph_tmp.drawImage(img, affineTransform, null);
        		this.tricker = this.TRICKER;	
              	this.rotation += 26;
        	}	
        	g.drawImage(image, this.getPosition().getIntX()-this.getRadius(),this.getPosition().getIntY()-this.getRadius(), null);
        	if(rotation >= 360){
        		attacking = false;
        		Level.getInstance().removeObject(this);
        	} 
             this.tricker--;

        }
	}
	
	public void execute() {
		if(!this.attacking && Level.getInstance().mousePressed()&& pl.getBall().isMoving()){
			if(this.attacks > 0){
				this.attacking = true;
				this.rotation = 0;
				this.tricker = 0;
				this.attacks--;
				this.pl.setWeaponState();
				Level.getInstance().registerObject(this);
				return;
			} else {
				System.err.println("out of attacks!");
			}
		}
		if(this.attacking){
			for(int i = 0; i < Level.getInstance().getObjects().size(); i++){
				if(Level.getInstance().getObjects().get(i) instanceof Ball){
					Ball enemy = (Ball) Level.getInstance().getObjects().get(i);
					if(enemy.getPlayer() != this.getPlayer()&& enemy.isVisible()){
						double dist;
						if((dist = this.getPosition().getDist(enemy)) <= this.getRadius()+enemy.getRadius()){
							Point p = this.getPosition().sub(enemy.getPosition());
							double angle = Math.atan2(p.getY(), p.getX());
							angle = (angle*180/Math.PI) - (this.rotation-180);
							if(Math.abs(angle)<15){
								int destr = (int) ((40-dist)/2);
								pl.addDamage(destr);
								enemy.getPlayer().setHP(enemy.getPlayer().getHP()-destr);
							}
						}
							
					}
				}
			}
		}

	}
	public boolean newMotion(WorldObject object){
		return true;
	}	
	public ImageIcon getIcon() {
		return Icon;//ImageStore.getInstance().getIcon("dolf/objects/weapons/Sword/gfx/icon.png");	
	}

	public String getName() {
		return Name;
		/*if (messages == null) loadProperties();
		return messages.getString("name");*/
	}
	
	public String getDescription() {
		return Description;
		/*if (messages == null) loadProperties();
		return messages.getString("description");*/
	}
	public String getType(){
		return WeaponType;
	}
		
	public int getPrice() {
		return Price;
	}

	public void remove(){
		Level.getInstance().removeObject(this);
		pl.removeWeapon(this);
		this.pl.setWeaponState();
		this.pl.setAktWeapon(null);
	}
	public Point getPosition(){
		return pl.getBall().getPosition();
	}
	public Player getPlayer(){
		return this.pl;
	}
	public double getState(){
		return (double)this.attacks/this.MAX_ATTACK;
	}
}

